package com.back_end.NetWork.chart;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UdpReceiveChart01 {
    public static void main(String[] args) throws Exception{
        DatagramSocket socket = new DatagramSocket(6666);

        while(true){
            //准备接收包裹
            byte[] container = new byte[1024];

            DatagramPacket packet = new DatagramPacket(container,0,container.length);
            socket.receive(packet); //阻塞式接收包裹
            //断开连接
            byte[] data = packet.getData();   //取到数据
            String receiveData = new String(data,0, packet.getLength());  //此时取packet的length，因为取字节流的那便是1024个长度
            System.out.println(receiveData);
            //判断遇到bye，断开连接
            if(receiveData.equals("bye")){
                break;
            }
        }
        socket.close();
    }
}
